<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="车辆号" prop="carCode">
        <el-input
          v-model="queryParams.carCode"
          placeholder="请输入车辆号"
          clearable
          maxlength="20"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作业人" prop="userName">
        <el-input
          maxlength="20"
          show-word-limit
          v-model="queryParams.userName"
          placeholder="请输入作业人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作业时间" prop="dateRange">
        <el-date-picker
          clearable
          v-model="dateRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['TWorkRecordAbnormal:abnormal:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="abnormalList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="55" />
      <el-table-column
        label="日期"
        align="center"
        prop="jobDate"
        width="130"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jobDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="班次"
        align="center"
        prop="shift"
        width="80"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        label="台位"
        align="center"
        prop="positionName"
        width="100"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="股道"
        align="center"
        prop="trackName"
        width="80"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="列位"
        align="center"
        prop="complete"
        width="80"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="车辆号"
        align="center"
        prop="carCode"
        width="150"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="计划开始时间"
        align="center"
        prop="planStartTime"
        width="180"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="计划结束时间"
        align="center"
        prop="planEndTime"
        width="180"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="作业班组"
        align="center"
        prop="workTeamName"
        width="130"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="作业人"
        align="center"
        prop="userName"
        width="100"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="作业时间"
        align="center"
        prop="jobTime"
        width="180"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="允许波动时间"
        align="center"
        prop="allowTime"
        width="130"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="作业状态"
        align="center"
        prop="jobStatus"
        width="100"
        :show-overflow-tooltip="true"
      />

      <!-- <el-table-column label="挂号志人" align="center" prop="insertUserName" />
      <el-table-column label="撤号志人" align="center" prop="removeUserName" />
      <el-table-column label="作业开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="作业结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="作业类型：1供电预检，2断电作业，3供电检修，4无车作业" align="center" prop="type" /> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['TWorkRecordAbnormal:abnormal:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['TWorkRecordAbnormal:abnormal:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改号志异常数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="计划guid" prop="planGuid">
          <el-input v-model="form.planGuid" placeholder="请输入计划guid" />
        </el-form-item>
        <el-form-item label="列位" prop="complete">
          <el-input v-model="form.complete" placeholder="请输入列位" />
        </el-form-item>
        <el-form-item label="日期" prop="jobDate">
          <el-date-picker
            clearable
            v-model="form.jobDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="班次，1白班，2夜班" prop="shift">
          <el-input
            v-model="form.shift"
            placeholder="请输入班次，1白班，2夜班"
          />
        </el-form-item>
        <el-form-item label="台位名称" prop="positionName">
          <el-input v-model="form.positionName" placeholder="请输入台位名称" />
        </el-form-item>
        <el-form-item label="股道名称" prop="trackName">
          <el-input v-model="form.trackName" placeholder="请输入股道名称" />
        </el-form-item>
        <el-form-item label="计划开始时间" prop="planStartTime">
          <el-input
            v-model="form.planStartTime"
            placeholder="请输入计划开始时间"
          />
        </el-form-item>
        <el-form-item label="计划结束时间" prop="planEndTime">
          <el-input
            v-model="form.planEndTime"
            placeholder="请输入计划结束时间"
          />
        </el-form-item>
        <el-form-item label="车辆号" prop="carCode">
          <el-input v-model="form.carCode" placeholder="请输入车辆号" />
        </el-form-item>
        <el-form-item label="班组名称" prop="workTeamName">
          <el-input v-model="form.workTeamName" placeholder="请输入班组名称" />
        </el-form-item>
        <el-form-item label="挂号志人" prop="insertUserName">
          <el-input
            v-model="form.insertUserName"
            placeholder="请输入挂号志人"
          />
        </el-form-item>
        <el-form-item label="撤号志人" prop="removeUserName">
          <el-input
            v-model="form.removeUserName"
            placeholder="请输入撤号志人"
          />
        </el-form-item>
        <el-form-item label="作业开始时间" prop="startTime">
          <el-date-picker
            clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择作业开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="作业结束时间" prop="endTime">
          <el-date-picker
            clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择作业结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="允许波动时间" prop="allowTime">
          <el-input v-model="form.allowTime" placeholder="请输入允许波动时间" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAbnormal,
  getAbnormal,
  delAbnormal,
  addAbnormal,
  updateAbnormal,
} from "@/api/TWorkRecordAbnormal/abnormal";

export default {
  name: "Abnormal",
  data() {
    return {
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 号志异常数据表格数据
      abnormalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carCode: null,
        userName: null,
        beginTime: null,
        endTimes: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shift: [
          {
            required: true,
            message: "班次，1白班，2夜班不能为空",
            trigger: "blur",
          },
        ],
        insertUserName: [
          { required: true, message: "挂号志人不能为空", trigger: "blur" },
        ],
        type: [
          {
            required: true,
            message:
              "作业类型：1供电预检，2断电作业，3供电检修，4无车作业不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询号志异常数据列表 */
    getList() {
      this.loading = true;
      listAbnormal(this.queryParams).then((response) => {
        this.abnormalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        planGuid: null,
        complete: null,
        jobDate: null,
        shift: null,
        positionName: null,
        trackName: null,
        planStartTime: null,
        planEndTime: null,
        carCode: null,
        workTeamName: null,
        insertUserName: null,
        removeUserName: null,
        startTime: null,
        endTime: null,
        type: null,
        allowTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log(this.dateRange)
      this.queryParams.beginTime = this.dateRange[0];
      this.queryParams.endTimes = this.dateRange[1];
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加号志异常数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAbnormal(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改号志异常数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAbnormal(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAbnormal(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除号志异常数据编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAbnormal(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "TWorkRecordAbnormal/abnormal/export",
        {
          ...this.queryParams,
        },
        `超时记录_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
